﻿// Sorting an array means to arrange its elements in increasing order. Write a program to sort an array. 
// Use the "selection sort" algorithm

using System;

class SelectionSortOfArray
{
    static void Main()
    {
        // read array size
        uint arraySize;
        do
        {
            Console.Write("Enter array size: ");
        } while (!uint.TryParse(Console.ReadLine(), out arraySize));

        // read array elements
        int[] intArray = new int[arraySize];
        for (int i = 0; i < intArray.Length; i++)
        {
            do
            {
                Console.Write("Enter array elements {0}: ", i + 1);
            } while (!int.TryParse(Console.ReadLine(), out intArray[i]));
        }

        // selection sort
        int minIndex;
        for (int index = 0; index < intArray.Length - 1; index++)
        {
            minIndex = index;
            for (int indexRest = index + 1; indexRest < intArray.Length; indexRest++)
            {
                // search for min elements from the rest
                if (intArray[indexRest] < intArray[minIndex])
                {
                    minIndex = indexRest;
                }
            }

            if (minIndex != index)
            {
                // swap elements of the array
                int tempElement = intArray[index];
                intArray[index] = intArray[minIndex];
                intArray[minIndex] = tempElement;
            }
        }

        // print sorted array
        Console.Write("Sorted Array: {");
        for (int i = 0; i < intArray.Length; i++)
        {
            Console.Write(intArray[i]);
            if (i != intArray.Length - 1)
            {
                Console.Write(", ");
            }
        }
        Console.WriteLine("}");
    }
}